package cn.i7baozh.boot.learn.thirdpropertyV2;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.PropertySource;

/**
 * @author zhangbaoqi532
 * @date 2025/3/17
 */
@Slf4j
public class MySourceV2 extends PropertySource<String> implements ApplicationContextAware {

    private MySourceV2Service mySourceV2Service;
    private ApplicationContext applicationContext;

    public MySourceV2(String name) {
        super(name);
    }

    @Override
    public Object getProperty(String key) {
        if (mySourceV2Service == null) {
            mySourceV2Service = applicationContext.getBean(MySourceV2Service.class);
        }
//        log.info("**获取配置 {} ", key );
        return mySourceV2Service.getProperty(key);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        log.info("******** 初始化 applicationContext");
    }
}
